package com.atguigu.ssyx.product.api;


import com.atguigu.ssyx.model.product.Category;
import com.atguigu.ssyx.model.product.SkuInfo;
import com.atguigu.ssyx.product.service.ICategoryService;
import com.atguigu.ssyx.product.service.ISkuInfoService;
import com.atguigu.ssyx.vo.product.SkuInfoVo;
import com.atguigu.ssyx.vo.product.SkuStockLockVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/product")
public class ProductInnnerController {

    @Autowired
    private ICategoryService categoryService;

    @Autowired
    private ISkuInfoService skuInfoService;

    @GetMapping("inner/getCategory/{categoryId}")
    public Category getCategory(@PathVariable Long categoryId) {
        Category category = categoryService.getById(categoryId);
        return category;
    }

    @GetMapping("inner/getSkuInfo/{skuId}")
    public SkuInfo getSkuInfo(@PathVariable Long skuId) {
        SkuInfo skuInfo = skuInfoService.getSkuInfo(skuId);
        return skuInfo;
    }

    @PostMapping("inner/findsSkuInfoList")
    public List<SkuInfo> findsSkuInfoList(@RequestBody List<Long> skuIdList) {
        List<SkuInfo> skuInfoList = skuInfoService.findsSkuInfoList(skuIdList);
        return skuInfoList;
    }

    @ApiOperation(value = "批量获取分类信息")
    @PostMapping("inner/findCategoryList")
    public List<Category> findCategoryList(@RequestBody List<Long> categoryIdList) {
        return categoryService.listByIds(categoryIdList);
    }

    @GetMapping("/inner/findSkuInfoByKeyword/{keyword}")
    public List<SkuInfo> findSkuInfoByKeyword(@PathVariable("keyword") String keyword) {
        List<SkuInfo> list = skuInfoService.findSkuInfoByKeyword(keyword);
        System.out.println("list:" + list);
        return list;
    }

    /**
     * 找到所有的分类
     */

    @GetMapping("/inner/findAllCategoryList")
    public List<Category> findAllCategoryList() {
        List<Category> categoryList = categoryService.list(new LambdaQueryWrapper<Category>().orderByAsc(Category::getSort));
        return categoryList;
    }


    @GetMapping("inner/findNewPersonSkuInfoList")
    public List<SkuInfo> findNewPersonSkuInfoList() {
//        查找新人专享商品。
        List<SkuInfo> skuInfoList = skuInfoService.findNewPersonSkuInfoList();
        return skuInfoList;
    }

    /**
     *  获得详细数据
     */
    @ApiOperation(value = "根据skuId获取sku信息")
    @GetMapping("/inner/getSkuInfoVo/{skuId}")
    public SkuInfoVo getSkuInfoVo(@PathVariable("skuId") Long skuId) {
        return skuInfoService.getSkuInfoVo(skuId);
    }

    @ApiOperation(value = "锁定库存")
    @PostMapping("inner/checkAndLock/{orderNo}")
    public Boolean checkAndLock(@RequestBody List<SkuStockLockVo> skuStockLockVoList, @PathVariable String orderNo) {
        return skuInfoService.checkAndLock(skuStockLockVoList, orderNo);
    }
}
